//
//  SEGActivityListViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/11.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityListViewController.h"
#import "SEGActivityListCell.h"
#import "SEGActivityDetailBaseViewController.h"
#import "SEGActivityCollectViewController.h"
#import "SEGActivityOffLineViewController.h"
#import "SEGActivityLotteryViewController.h"
#import "SEGActivityTriggerViewController.h"
#import "SEGActivitySignViewController.h"

#import "SEGActivityLotteryBubbleButton.h"


@interface SEGActivityListViewController ()<UITableViewDelegate, UITableViewDataSource>


/**
 主视图
 */
@property (nonatomic, strong)UITableView *tableView;


@property (nonatomic, strong) SEGActivityListViewModel *viewModel;

@end

@implementation SEGActivityListViewController

- (instancetype)initWithIsRecommand:(BOOL)isRecommand isMine:(BOOL)isMine {
    self = [super init];
    if (self) {
        self.viewModel = [[SEGActivityListViewModel alloc] initWithIsRecommand:isRecommand isMine:isMine];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = kColor_bg;
    [self.view addSubview:self.tableView];
    
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [weakSelf headerRereshing];
    } footerRefreshBlock:^{
        [weakSelf footerRereshing];
    }];
    [self.tableView beginHeaderRefreshing];
    
    //    [self setupRefresh];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.displayTitle.length > 0) {
        self.title = self.displayTitle;
        self.displayTitle = nil;
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [SEGActivityLotteryBubbleButton hiddenLotteryBubble];
    [super viewWillDisappear:animated];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//头部刷新逻辑
- (void)headerRereshing {
    [self requsetActivitysWithRefresh:YES];
}

//底部刷新逻辑
- (void)footerRereshing {
    [self requsetActivitysWithRefresh:NO];
}

- (void)requsetActivitysWithRefresh:(BOOL)refresh {
    //获取活动列表
    [[self.viewModel.listCommand execute:[[NSNumber alloc] initWithBool:refresh]] subscribeNext:^(id  _Nullable x) {
        [self.tableView stopAnimatingWindmill];
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    } completed:^{
        // 显示到界面上
        if (self.viewModel.activityManager.activityList.count == 0) {
            NSDictionary *params =@{@"view":self.view};
            [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:params];
        }
        else {
            [[SEGUhomeMediator sharedInstance] seg_hideCustomProgressImage:@{@"view":self.view}];
        }
        [self.tableView reloadData];
    }];

}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.activityManager.activityList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGActivityListCell *cell = [tableView dequeueReusableCellWithIdentifier:kActivityListCellIdentifier forIndexPath:indexPath];
    if (self.viewModel.activityManager.activityList.count > indexPath.section) {
        [cell setUpCellWithModel:self.viewModel.activityManager.activityList[indexPath.section]];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.1;
    } else {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SEGActivityDetailBaseViewController *vc;
    SEGActivityListModel *model = self.viewModel.activityManager.activityList[indexPath.section];
    SEGActivityType actType = [model.actType integerValue];
    switch (actType) {//活动类型(1:征集类,2:抽奖类,3:投票类,4:调查类,5:线下类,6:签到类,7:触发类)
        case SEGActivityType_Collect: /*征集类*/
        {
            vc = [[SEGActivityCollectViewController alloc] init];
            break;
        }
        case SEGActivityType_Lottery: /*抽奖类*/
        {
            vc = [[SEGActivityLotteryViewController alloc] init];
            break;
        }
        case SEGActivityType_OffLine: /*线下类*/
        {
            vc = [[SEGActivityOffLineViewController alloc] init];
            break;
        }
        case SEGActivityType_Sign: /*签到类*/
        {
            SEGActivitySignViewController *vc = [[SEGActivitySignViewController alloc] init];
            vc.actId = model.actId;
            [self.navigationController pushViewController:vc animated:YES];
            //            break;
            return;
        }
        case SEGActivityType_Trigger: /*触发类*/
        {
            vc = [[SEGActivityTriggerViewController alloc] init];
            break;
        }
    }
    vc.actId = self.viewModel.activityManager.activityList[indexPath.section].actId;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-kNavigationBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.separatorColor = kColor_L2;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[SEGActivityListCell class] forCellReuseIdentifier:kActivityListCellIdentifier];
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}


@end

